package demo4;
//浅拷贝
//class Money {
//    public double money = 12.5;
//}
//
//class Student implements Cloneable {
//    public String name;
//    public Money m = new Money();
//
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                '}';
//
//    }
//}
//public class Test {
//    public static void main(String[] args) throws CloneNotSupportedException {
//        Student student1 = new Student();
//        student1.name = "John";
//        Student student2 = (Student)student1.clone();//向下转型
//        System.out.println(student1);
//        System.out.println(student2);
//        System.out.println(student1.m.money);
//        System.out.println(student2.m.money);
//        System.out.println("=============================");
//        student1.m.money = 9.5;
//        System.out.println(student1.m.money);
//        System.out.println(student2.m.money);
//    }
//}


//深拷贝（当前对象的每一个对象都要克隆）
//class Money implements Cloneable {
//    public double money = 12.5;
//
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }
//}
//
//class Student implements Cloneable {
//    public String name;
//    public Money m = new Money();
//
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        Student s = (Student) super.clone();//只是克隆了Student对象
//        s.m = (Money)this.m.clone();//克隆了Student对象中的Money对象
//        return s;
//    }
//}
//
//public class Test{
//    public static void main(String[] args) throws CloneNotSupportedException {
//        Student s1 = new Student();
//        s1.name = "zhangsan";
//        Student s2 = (Student) s1.clone();
//        System.out.println(s1.name);
//        System.out.println(s2.name);
//        System.out.println(s1.m.money);
//        System.out.println(s2.m.money);
//        System.out.println("========================");
//        s2.m.money = 99;
//        System.out.println(s1.m.money);
//        System.out.println(s2.m.money);
//    }
//}




























